---
title: "Shell Script Snippets"
date: 2019-12-26
categories:
- linux
- shell
tags:
---

<div id="content">
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#orgf9a09a1">Scripts</a>
<ul>
<li><a href="#org3b10ebe">输出第n行</a></li>
<li><a href="#org5f1ec3e">逐行读取文件</a></li>
<li><a href="#org89b5e84">检查上一个命令是否成功</a></li>
</ul>
</li>
<li><a href="#orgc46f53f">FAQ</a>
<ul>
<li><a href="#org95c3d81">echo 换行丢失，成了一整行</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="outline-2" id="outline-container-orgf9a09a1">
<h2 id="orgf9a09a1">Scripts</h2>
<div class="outline-text-2" id="text-orgf9a09a1">
</div>
<div class="outline-3" id="outline-container-org3b10ebe">
<h3 id="org3b10ebe">输出第n行</h3>
<div class="outline-text-3" id="text-org3b10ebe">
<div class="org-src-container">
<pre class="src src-sh"><span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">awk</span>
<span style="font-weight: bold;">echo</span> <span style="font-style: italic;">"$files"</span> | awk <span style="font-style: italic;">'NR==3 {print $0}'</span>

<span style="font-weight: bold; font-style: italic;"># </span><span style="font-weight: bold; font-style: italic;">sed</span>
ls -l | sed -n 2p
</pre>
</div>
</div>
</div>
<div class="outline-3" id="outline-container-org5f1ec3e">
<h3 id="org5f1ec3e">逐行读取文件</h3>
<div class="outline-text-3" id="text-org5f1ec3e">
<blockquote>
<p>
<a href="https://stackoverflow.com/questions/7427262/how-to-read-a-file-into-a-variable-in-shell">https://stackoverflow.com/questions/7427262/how-to-read-a-file-into-a-variable-in-shell</a>
</p>
</blockquote>
<div class="org-src-container">
<pre class="src src-bash"><span style="font-weight: bold;">while </span><span style="font-weight: bold;">read</span> line; <span style="font-weight: bold;">do</span>    
    <span style="font-weight: bold;">echo</span> $<span style="font-weight: bold; font-style: italic;">line</span>    
<span style="font-weight: bold;">done</span> &lt; file.txt
</pre>
</div>
</div>
</div>
<div class="outline-3" id="outline-container-org89b5e84">
<h3 id="org89b5e84">检查上一个命令是否成功</h3>
<div class="outline-text-3" id="text-org89b5e84">
<div class="org-src-container">
<pre class="src src-sh">some_command
<span style="font-weight: bold;">if</span> [ $<span style="font-weight: bold; font-style: italic;">?</span> -eq 0 ]; <span style="font-weight: bold;">then</span>
    <span style="font-weight: bold;">echo</span> OK
<span style="font-weight: bold;">else</span>
    <span style="font-weight: bold;">echo</span> FAIL
<span style="font-weight: bold;">fi</span>
</pre>
</div>
</div>
</div>
</div>
<div class="outline-2" id="outline-container-orgc46f53f">
<h2 id="orgc46f53f">FAQ</h2>
<div class="outline-text-2" id="text-orgc46f53f">
</div>
<div class="outline-3" id="outline-container-org95c3d81">
<h3 id="org95c3d81">echo 换行丢失，成了一整行</h3>
<div class="outline-text-3" id="text-org95c3d81">
<p>
echo $files # 输出没有换行，使用awk等命令被作为整行
echo "$files" # 原样输出
</p>
<p>
echo 命令原型是 echo [SHORT-OPTION]… [STRING]… 如果不加引号，后面的字符串会作为多个参数传递给echo，echo顺序输出各个参数。
如果加了引号，字符串则是一整个参数
</p>
</div>
</div>
</div>
</div>
<div class="status" id="postamble">
<p class="date">Date: 2019-12-26</p>
<p class="author">Author: gdme1320</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
